package cn.rengy.isql.config;

import cn.rengy.auth.UserService;
import cn.rengy.isql.*;
import cn.rengy.isql.transcode.NoTranscoder;
import cn.rengy.lang.CacheService;
import cn.rengy.lang.ObjectTranscoder;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;


@AutoConfiguration
public class IsqlAutoConfiguration {


    @ConditionalOnMissingBean(ObjectTranscoder.class)
    @Bean
    public ObjectTranscoder objectTranscoder() {
        return new NoTranscoder();
    }

    @ConditionalOnMissingBean(CacheService.class)
    @Bean
    public CacheService cacheService() {
        return new ConcurrentMapCacheService();
    }
    @ConditionalOnMissingBean(UserService.class)
    @Bean
    public UserService userService() {
        return new NoUserService();
    }

    @Bean
    public DbSqlValueService dbSqlValueService(Dao dao, CacheService cacheService) {
        return new DbSqlValueService(dao,cacheService);
    }

    @Bean
    public SqlRoleService sqlRoleService(Dao dao, CacheService cacheService) {
        return new SqlRoleService(dao,cacheService);
    }

    @Bean
    public GetSqlService getSqlService(UserService userService, SqlRoleService sqlRoleService, DbSqlValueService dbSqlValueService) {
        return new GetSqlService(userService,sqlRoleService,dbSqlValueService);
    }

    @Bean
    public Dao dao(JdbcTemplate jdbcTemplate) {
        return new Dao(jdbcTemplate);
    }


    @Bean
    public NamedSqlDao namedSqlDao(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        return new NamedSqlDao(namedParameterJdbcTemplate);
    }


    @Bean
    public DbService dbService(Dao dao, NamedSqlDao namedSqlDao, CacheService cacheService, ObjectTranscoder objectTranscoder, GetSqlService getSqlService) {
        return new DbService( dao,  namedSqlDao,  cacheService,  objectTranscoder,  getSqlService);
    }
}
